<template>
  <div class="employees-container">
    <el-tabs v-model="activeName">
      <el-tab-pane label="员工列表" name="first" />
    </el-tabs>
    <el-button
      type="primary"
      size="small"
      icon="el-icon-plus"
      @click="addRole"
    >新增</el-button>
    <el-button
      type="warning"
      size="small"
      icon="el-icon-download"
      style="background-color:#ff5722;color:#fff"
      @click="EmployExport"
    >导出</el-button>
    <el-button
      v-if="isShowByMenu('import')"
      type="warning"
      size="small"
      icon="el-icon-upload"
      style="background-color:#ffb800;color:#fff"
      @click="toImport"
    >导入</el-button>
    <el-table v-loading="loading" :data="tableData" border style="width: 100%">
      <el-table-column
        align="center"
        type="index"
        sortable
        label="序号"
        width="100"
      />
      <el-table-column
        align="center"
        prop="username"
        sortable
        label="姓名"
        width="100"
      />

      <el-table-column
        align="center"
        prop="mobile"
        sortable
        label="手机号"
        width="140"
      />
      <el-table-column
        align="center"
        prop="workNumber"
        sortable
        label="工号"
        width="80"
      />
      <el-table-column
        align="center"
        prop="formOfEmployment"
        sortable
        label="聘用形式"
        width="110"
        :formatter="formatter"
      />
      <el-table-column
        align="center"
        prop="departmentName"
        sortable
        label="部门"
        width="100"
      />
      <el-table-column
        align="center"
        prop="timeOfEntry"
        sortable
        label="入职时间"
      >
        <template #default="{row}">
          {{ row.timeOfEntry | formatDate }}
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="240">
        <template #default="scope">
          <el-button
            v-if="isShowByMenu('detail')"
            size="mini"
            type="text"
            @click="lookInfo(scope.row.id)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            @click="positive(scope.row)"
          >转正</el-button>
          <el-button
            size="mini"
            type="text"
            @click="OnodjustPost(scope.row.id)"
          >调岗</el-button>
          <el-button size="mini" type="text">离职</el-button>
          <el-button
            size="mini"
            type="text"
            @click="assignRole(scope.row)"
          >角色</el-button>
          <el-button
            v-if="isDelByMenu('employees_delete')"
            size="mini"
            type="text"
            @click="delEmployees(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page.sync="page"
        :page-size="params.pagesize"
        layout="total, prev, pager, next"
        :total="params.total"
      />
    </div>
    <addDialog ref="addDialog" />
    <assignRoles ref="assignRoles" />
    <positiveDialog ref="positiveDialog" />
  </div>
</template>

<script>
import addDialog from './components/addDialog.vue'
import assignRoles from './components/assignRoles.vue'
import positiveDialog from './components/positiveDialog.vue'
import { getEmployeeList, getEssentialById } from '@/api/employee'
import { getRoleList } from '@/api/role'
import { delEmployeeById } from '@/api/employee'
import EmployeeEunm from '@/api/constant/employees'
import mixins from '@/mixins'
export default {
  name: 'Employees',
  components: {
    addDialog,
    assignRoles,
    positiveDialog
  },
  mixins: [mixins],
  data() {
    return {
      activeName: 'first',
      tableData: [],
      params: {
        pagesize: 10,
        total: 0
      },
      page: 1,
      loading: false
    }
  },
  watch: {
    page() {
      this.getList()
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 查看
    lookInfo(id) {
      // id在路由传 就得使用name
      this.$router.push({ name: 'detail', params: { id }})
    },
    // 调岗
    OnodjustPost(id) {
      // id在路由传 就得使用name
      this.$router.push({ name: ' adjustPost', params: { id }})
    },
    // 查询员工列表
    async getList() {
      this.loading = true
      const res = await getEmployeeList({
        page: this.page,
        size: this.params.pagesize
      })
      this.tableData = res.rows
      this.params.total = res.total
      setTimeout(() => {
        this.loading = false
      }, 300)
    },
    // 新增点击事件
    addRole() {
      this.$refs.addDialog.showDialog = true
    },
    // 页码的点击事件
    handleCurrentChange(val) {
      this.params.page = val
      this.getList()
    },
    // 分配角色
    async assignRole(row) {
      this.$refs.assignRoles.employeesId = row.id
      // 查询角色列表
      const res = await getRoleList({ page: 1, pagesize: 20 })
      // 根据id查询员工基本信息
      const checkRes = await getEssentialById({ id: row.id })
      this.$refs.assignRoles.RoleList = res.rows
      this.$refs.assignRoles.checkList = checkRes.roleIds
      this.$refs.assignRoles.showDialog = true
    },
    // 自定义列
    formatter(row) {
      const res = EmployeeEunm.hireType.find(v => {
        return v.id === +row.formOfEmployment
      })
      return res.value
    },
    // 删除员工
    delEmployees(id) {
      this.$confirm('是否确认删除此员工?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delEmployeeById(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
        .catch(() => {})
    },
    // 转正
    positive(row) {
      this.$refs.positiveDialog.positiveList = row
      this.$refs.positiveDialog.showDialog = true
    },
    // 导入
    toImport() {
      this.$router.push({ path: '/import' })
    },
    // 导出
    EmployExport() {
      import('@/vendor/Export2Excel').then(async excel => {
        // 表头
        const tHeader = Object.keys(this.tableData[0])
        const data = []
        const res = await getEmployeeList({
          page: this.page,
          size: this.params.total
        })
        // 循环数据 item是每一个对象
        res.rows.forEach(item => {
          const _arr = []
          // 去除每个对象的属性值 循环这个属性值
          Object.keys(item).forEach(key => {
            // 将对象属性所对应的值添加到数组里
            _arr.push(item[key])
          })
          // 将所有处理的数据添加到data
          data.push(_arr)
        })

        excel.export_json_to_excel({
          header: tHeader, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
      })
    }
  }
}
</script>
<style>
.el-select-dropdown__item.selected {
  color: #00a6a6 !important;
}
</style>
<style lang="scss">
.employees-container {
  padding: 50px 15px 55px 60px;
  background-color: #fff;
  border-radius: 5px;
  .el-tree {
    position: absolute;
    z-index: 1;
    margin-top: 4px;
    width: 37%;
    border-radius: 3px;
    border: 1px solid #e4e7ed;
  }
  .el-table__header {
    th {
      background-color: #fafafa;
      border-bottom: 2px solid #e4e7ed;
    }
  }
  .info-box {
    display: inline-block;
    height: 100%;
    border: 1px solid #91d5ff;
    border-radius: 4px;
    line-height: 32px;
    padding: 2px 10px;
    background-color: #e6f7ff;
    i {
      margin-right: 5px;
      color: #409eff;
      font-size: 20px;
    }
    span {
      font-size: 14px;
    }
  }
  .el-checkbox-group {
    label {
      .el-checkbox__input.is-checked + .el-checkbox__label {
        color: #00a6a6;
      }
      .el-checkbox__input.is-checked .el-checkbox__inner {
        background-color: #00a6a6;
        border-color: #00a6a6;
      }
    }
  }
  .el-input__inner:focus {
    border-color: #00a6a6 !important;
  }
  .el-tabs__item.is-active,
  .el-tabs__item:hover,
  .el-button,
  .el-pager li.active {
    color: #00a6a6;
  }

  .el-button--primary,
  .el-dialog__title,
  .el-dialog__close {
    background-color: #00a6a6;
    color: #fff;
    border-color: transparent;
  }

  .el-tabs__active-bar,
  .el-dialog__header {
    background-color: #00a6a6;
  }
  .el-button--small,
  .el-pagination {
    margin: 10px 0 13px 15px;
    float: right;
  }
}
</style>
